package com.xayy.ha.gen.lite.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xayy.ha.gen.lite.entity.GenTabField;
import com.xayy.ha.gen.lite.entity.GenTabInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClasssName CodeGenerateMapper
 * @Description 代码生成mapper
 * @Author Liuyh
 * @Date 2021/5/12
 * @Version V0.0.1
 */
@Mapper
@Component
public interface MysqlDbTableMapper {
    /**
     * 分页查询表格
     *
     * @param page      分页信息
     * @param tableName 表名称
     * @return
     */
    @Select({"<script>" +
            " select " +
            " table_name tableName, " +
            " engine, " +
            " table_comment tableComment, " +
            " table_collation tableCollation, " +
            " DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') createTime " +
            " from information_schema.tables " +
            " where table_schema = (select database()) " +
            " <if test=\"tableName != null and tableName.trim() != ''\"> " +
            "     and table_name like concat('%', #{tableName}, '%') " +
            " </if> " +
            " order by create_time desc " +
            "</script>"})
    IPage<List<Map<String, Object>>> selectTableList(Page page, @Param("tableName") String tableName);

    /**
     * 查询表信息
     *
     * @param tableName 表名称
     * @return
     */
    @Select("" +
            " select table_name tableName, " +
            "  engine, table_comment tableComment, " +
            "  DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') createTime " +
            "  from information_schema.tables " +
            " where table_schema = (select database()) and table_name = #{tableName}" +
            "")
    GenTabInfo selectTableInfo(@Param("tableName") String tableName);

    /**
     * 分页查询表分页信息
     *
     * @param page
     * @param tableName
     * @param dsName
     * @return
     */
    @Select("" +
            "SELECT " +
            "    column_name columnName, " +
            "    data_type dataType, " +
            "    column_comment fieldComment, " +
            "    column_key columnKey, " +
            "    extra, " +
            "    is_nullable AS isNullable, " +
            "    column_type AS columnType " +
            "FROM " +
            "    information_schema.COLUMNS " +
            "WHERE " +
            "    table_name = #{tableName} and table_schema = (select database()) order by ordinal_position" +
            "")
    IPage<GenTabField> selectTableColumnPage(Page page, @Param("tableName") String tableName, @Param("dsName") String dsName);


    @Select("" +
            "SELECT " +
            "    column_name columnName, " +
            "    data_type dataType, " +
            "    column_comment fieldComment, " +
            "    column_key columnKey, " +
            "    extra, " +
            "    is_nullable AS isNullable, " +
            "    column_type AS columnType " +
            "FROM " +
            "    information_schema.COLUMNS " +
            "WHERE " +
            "    table_name = #{tableName} and table_schema = (select database()) order by ordinal_position" +
            "")
    List<GenTabField> selectTableColumn(@Param("tableName") String tableName);
}